<template>
    <div>
      <el-dialog
      class="dialog"
       :title="id? '修改文章':'新增文章'"
       :visible='add'
       @close='close'
       @open='open'
        >
        <el-form
        :model="ruleForm"
        ref="ruleForm"
        label-width="100px"
        :rules="rules"
        class="demo-ruleForm">
  <el-form-item label="文章标题" prop="title">
    <el-input v-model="ruleForm.title"></el-input>
  </el-form-item>
  <el-form-item label="文章内容" prop="articleBody">
        <div class="box3">
            <quill-editor
            ref="myTextEditor"
            v-model="ruleForm.articleBody"
            :options="editorOption"
            class="editor"></quill-editor>
        </div>
  </el-form-item>
  <el-form-item label="视频地址">
    <el-input v-model.number="ruleForm.videoURL"></el-input>
  <!-- <footer></footer> -->
  </el-form-item>
      <div style="margin-left:25%">
        <el-form-item>
          <el-button @click="close">取消</el-button>
        <el-button type="primary" @click="submitForm('ruleForm')">确定</el-button>
      </el-form-item>
      </div>
</el-form>
    </el-dialog>
    </div>
</template>
<script>
import 'quill/dist/quill.core.css'
import 'quill/dist/quill.snow.css'
import 'quill/dist/quill.bubble.css'
import { quillEditor } from 'vue-quill-editor'
// 请求
import { detail, update, add } from '@/api/hmmm/articles'
export default {
  // 工具栏配置
  props: {
    add: {
      type: Boolean,
      default: true
    },
    id: {
      type: Number
      // required: true
    }
  },
  data () {
    const toolbarOptions = [
      ['bold', 'italic', 'underline', 'strike'], // 加粗 斜体 下划线 删除线 -----['bold', 'italic', 'underline', 'strike']
      [{ list: 'ordered' }, { list: 'bullet' }], // 有序、无序列表-----[{ list: 'ordered' }, { list: 'bullet' }]
      ['blockquote'], // 引用  代码块-----['blockquote', 'code-block']
      ['image', 'code-block'] // 链接、图片、视频-----['link', 'image', 'video']
    ]
    return {
      editorOption: {
        //  富文本编辑器配置
        modules: {
          // 工具栏定义的
          toolbar: toolbarOptions
        },
        // 主题
        theme: 'snow',
        placeholder: '请输入正文'
      },
      ruleForm: {
        videoURL: '',
        title: '',
        articleBody: ''
      },
      rules: {
        articleBody: [{ required: true, message: '文章内容不能为空', trigger: 'blur' }],
        title: [{ required: true, message: '文章标题不能为空', trigger: 'blur' }]

      }
    }
  },
  components: {
    quillEditor
  },
  methods: {
    onEditorChange ({ editor, html, text }) {
      console.log(html)
      this.content = html
    },
    // 关闭
    close () {
      this.$emit('update:add', false)
      // 重置
      this.$refs.ruleForm.resetFields()
      this.ruleForm = {
        videoURL: '',
        title: '',
        articleBody: ''
      }
    },
    // 启动时触发
    async open () {
      // 判断状态
      if (this.id) {
        // 修改
        const { data } = await detail({ id: this.id })
        // console.log(data)
        this.ruleForm = data
      }
    },
    // 提交修改和添加
    async submitForm () {
      try {
        await this.$refs.ruleForm.validate()
      } catch {
        return false
      }
      if (this.id) {
        await update(this.ruleForm)
        this.$message.success('修改成功')
        // 更新
      } else {
        // 添加
        await add(this.ruleForm)
        this.$message.success('添加成功')
      }
      this.$parent.list()
      this.close()
    }
  }
}
</script>
<style lang="less" scoped>
.editor{
  height: 170px;

}
::v-deep{
  .el-dialog__body{
    height: 450px;
  }
}
.box3{
  height: 230px
}
</style>
